package com.css.zfzx.sjcj.modules.hddcwyGeophysvypoint.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.css.zfzx.sjcj.modules.hddcwyGeophysvypoint.repository.HddcWyGeophysvypointNativeRepository;
import com.css.zfzx.sjcj.modules.hddcwyGeophysvypoint.repository.HddcWyGeophysvypointRepository;
import com.css.zfzx.sjcj.modules.hddcwyGeophysvypoint.repository.entity.HddcWyGeophysvypointEntity;
import com.css.zfzx.sjcj.modules.hddcwyGeophysvypoint.service.HddcWyGeophysvypointService;
import com.css.zfzx.sjcj.modules.hddcwyGeophysvypoint.viewobjects.HddcWyGeophysvypointQueryParams;
import com.css.zfzx.sjcj.modules.hddcwyGeophysvypoint.viewobjects.HddcWyGeophysvypointVO;
import com.css.bpm.platform.api.local.PlatformAPI;
import com.css.bpm.platform.sys.dict.repository.entity.DictItemEntity;
import com.css.bpm.platform.utils.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zyb
 * @date 2020-12-01
 */
@Service
public class HddcWyGeophysvypointServiceImpl implements HddcWyGeophysvypointService {

	@Autowired
    private HddcWyGeophysvypointRepository hddcWyGeophysvypointRepository;
    @Autowired
    private HddcWyGeophysvypointNativeRepository hddcWyGeophysvypointNativeRepository;

    @Override
    public JSONObject queryHddcWyGeophysvypoints(HddcWyGeophysvypointQueryParams queryParams , int curPage,
                                                 int pageSize) {
        Page<HddcWyGeophysvypointEntity> hddcWyGeophysvypointPage =
                this.hddcWyGeophysvypointNativeRepository.queryHddcWyGeophysvypoints(queryParams , curPage, pageSize);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(hddcWyGeophysvypointPage);
        return jsonObject;
    }


    @Override
    public HddcWyGeophysvypointEntity getHddcWyGeophysvypoint(String id) {
        HddcWyGeophysvypointEntity hddcWyGeophysvypoint = this.hddcWyGeophysvypointRepository.findById(id).orElse(null);
         return hddcWyGeophysvypoint;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcWyGeophysvypointEntity saveHddcWyGeophysvypoint(HddcWyGeophysvypointEntity hddcWyGeophysvypoint) {
        String uuid = UUIDGenerator.getUUID();
        hddcWyGeophysvypoint.setId(uuid);
        hddcWyGeophysvypoint.setCreateUser(PlatformSessionUtils.getUserId());
        hddcWyGeophysvypoint.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        this.hddcWyGeophysvypointRepository.save(hddcWyGeophysvypoint);
        return hddcWyGeophysvypoint;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcWyGeophysvypointEntity updateHddcWyGeophysvypoint(HddcWyGeophysvypointEntity hddcWyGeophysvypoint) {
        hddcWyGeophysvypoint.setUpdateUser(PlatformSessionUtils.getUserId());
        hddcWyGeophysvypoint.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        this.hddcWyGeophysvypointRepository.save(hddcWyGeophysvypoint);
        return hddcWyGeophysvypoint;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteHddcWyGeophysvypoints(List<String> ids) {
        List<HddcWyGeophysvypointEntity> hddcWyGeophysvypointList = this.hddcWyGeophysvypointRepository.findAllById(ids);
        if(!PlatformObjectUtils.isEmpty(hddcWyGeophysvypointList) && hddcWyGeophysvypointList.size() > 0) {
            for(HddcWyGeophysvypointEntity hddcWyGeophysvypoint : hddcWyGeophysvypointList) {
                this.hddcWyGeophysvypointRepository.delete(hddcWyGeophysvypoint);
            }
        }
    }

    @Override
    public List<DictItemEntity> getValidDictItemsByDictCode(String dictCode) {
        List<DictItemEntity> validDictItems = PlatformAPI.getDictAPI().getValidDictItemsByDictCode(dictCode);
        return validDictItems;
    }

}
